/*
//
//              INTEL CORPORATION PROPRIETARY INFORMATION
//  This software is supplied under the terms of a license  agreement or
//  nondisclosure agreement with Intel Corporation and may not be copied
//  or disclosed except in  accordance  with the terms of that agreement.
//    Copyright (c) 2008 Intel Corporation. All Rights Reserved.
//
//
*/

#include "umc_defs.h"
#if defined(UMC_ENABLE_AVS_SPLITTER)

#include "umc_avs_splitter.h"
#include "umc_media_buffer.h"
#include "vm_time.h"

namespace UMC
{

enum
{
    TIME_TO_SLEEP               = 5
};

Ipp32u AVSSplitter::InternalThreadRoutine(void *pvParam)
{
    AVSSplitter *pParent = (AVSSplitter *) pvParam;
    Status umcRes;

    // check error(s)
    if (NULL == pParent)
        return 0x0a55bad;

    // main working cycle
    while (false == pParent->m_bQuit)
    {
        MediaData src;

        // lock the destination buffer
        umcRes = pParent->m_pVideoBuffer->LockInputBuffer(&src);

        // read the next video portion
        if (UMC_OK == umcRes)
        {
            Ipp32u nSize = (Ipp32u) src.GetBufferSize();

            // read data into buffer and unlock buffer
            umcRes = pParent->m_pDataReader->ReadData(src.GetBufferPointer(),
                                                      &nSize);
            src.SetDataSize(nSize);
            pParent->m_pVideoBuffer->UnLockInputBuffer(&src);
            // check end of stream
            if (UMC_OK != umcRes)
            {
                break;
            }
        }
        // the buffer is full, it is time to wait
        else if (UMC_ERR_NOT_ENOUGH_BUFFER == umcRes)
        {
            vm_time_sleep(TIME_TO_SLEEP);
        }
        // it is an error
        else
        {
            break;
        }
    }

    // set 'end of stream' state
    do
    {
        MediaData src;

        // lock the destination buffer
        umcRes = pParent->m_pVideoBuffer->LockInputBuffer(&src);
        if (UMC_OK == umcRes)
        {
            pParent->m_pVideoBuffer->UnLockInputBuffer(NULL, UMC_ERR_END_OF_STREAM);
            break;
        }

        // the buffer is full, it is time to wait
        vm_time_sleep(TIME_TO_SLEEP);

    } while (false == pParent->m_bQuit);

    return 0x0a55000;

} // Ipp32u AVSSplitter::InternalthreadRoutine(void *pvParam)

} // namespace UMC

#endif // #if defined(UMC_ENABLE_AVS_SPLITTER)
